package com.harzone.commons.search.result;

import java.util.ArrayList;
import java.util.List;

import com.harzone.commons.search.domain.Field;
import com.harzone.commons.search.domain.Record;

/**
 * 查询结果
 * @author ls
 * 2014年11月21日下午2:44:50
 */
public class SearchResult implements Cloneable{
	
	private long total = 0L;
	
	private Float maxScore = 0F;

	private List<Record> results = new ArrayList<Record>();

	
	public SearchResult() {
		super();
	}
	
	public SearchResult(long total) {
		super();
		this.total = total;
	}
 
	public boolean add(Record record){
		return results.add(record);
	}
	
	
	public List<Record> getResults() {
		return results;
	}

	
	public void setResults(List<Record> results) {
		this.results = results;
	}

	
	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public Float getMaxScore() {
		return maxScore;
	}

	public void setMaxScore(Float maxScore) {
		this.maxScore = maxScore;
	}

	@Override
	public String toString() {
		return "SearchResult [total=" + total + ", maxScore=" + maxScore
				+ ", results=" + results + "]";
	}

	/**
	 * 输出表结构
	 * @author ls
	 * 2014年11月24日下午1:49:14 
	 * @return
	 */
	public String printTable(){
		StringBuilder sb = new StringBuilder();
		sb.append("total="+total).append(",").append("maxScore="+maxScore).append("\n");
		if(!results.isEmpty()){
			Record record = results.get(0);
			List<Field> fs = record.getFields();
			int index = 0;
			for (Field field : fs) {
				if(index>0){
					sb.append(" | ");
				}
				sb.append(field.getShowName());
				index++;
			}
			sb.append("\n");
			for (Record record1 : results) {
				List<Field> fs1 = record1.getFields();
				int index1 = 0;
				for (Field field : fs1) {
					if(index1>0){
						sb.append(" | ");
					}
					sb.append(field.getValue());
					index1++;
				}
				sb.append("\n");
			}
		}
		return sb.toString();
	}
}
